<?php

namespace App\Http\Controllers\Admin\User;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Requests\Admin\User\User\AllotPermissionsRequest;
use App\Http\Requests\Admin\User\User\AllotRolesRequest;
use App\Http\Requests\Admin\User\User\CreateRequest;
use App\Http\Requests\Admin\User\User\UpdateRequest;
use App\Models\QuestionTag;
use App\Models\User;
use App\Repositories\Admin\User\UserRepository;
use Illuminate\Http\Request;

class UserController extends BaseController
{

    /**
     * TagController constructor.
     */
    public function __construct()
    {
        $this->repo = new UserRepository();
    }

    /**
     * 获取列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchList(Request $request)
    {
        $where = [];
        $params = $request->only('size,page');
        if ($request->has('level') && $request->get('level') > -1) {
            $where['level'] = $request->get('level', 1);
        }
        if ($request->has('status') && $request->get('status') > -1) {
            $where['status'] = $request->get('status', 1);
        }
        if (
        $request->has('start_time')&& $request->has('end_time')&&$request->get('start_time')&&$request->get('end_time')) {
            $params['between'][] = [
                'key' => 'created_at',
                'start' => $request->get('start_time'),
                'end' => $request->get('end_time')
            ];
        }
        $data = $this->repo->pagination($where, $params);
        return $this->success($data);
    }

    /**
     * 查单个信息
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function fetchInfo(User $user)
    {
        $user->load(['permissions', 'roles']);
        return $this->success($user);
    }

    /**
     * 创建
     * @param CreateRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(CreateRequest $request)
    {
        $data = $request->only('mobile', 'nickname', 'desc', 'password', 'status');
        $user = $this->repo->create($data);
        $user = $this->repo->model()->find($user->id);
        return !$user ? $this->error('添加失败') : $this->success($user, '添加成功');
    }

    /**
     * 更新用户
     * @param UpdateRequest $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(UpdateRequest $request, User $user)
    {
        $data = $request->only('nickname', 'desc', 'status');
        $user = $this->repo->update($user, $data);
        return !$user ? $this->error('更新失败') : $this->success($user, '更新成功');
    }

    /**
     * 删除
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function delete(User $user)
    {
        return $user->delete() ? $this->success([], '删除成功') : $this->error('删除失败');
    }

    /**
     * 重置密码
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function resetPassword(User $user)
    {
        $pwd = $data['password'] = config('admin.user_pwd');
        $saved = $this->repo->update($user, $data);
        return !$saved ? $this->error('重置失败') : $this->success([
            'password' => $pwd
        ], "密码已重置，重置密码为:{$pwd}");
    }

    /**
     * 修改状态
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function setStatus(User $user)
    {
        return $this->repo->update($user, ['status' => !$user->status]) ?
            $this->success([], '更新成功') : $this->error('更新失败');
    }

    /**
     * @param AllotRolesRequest $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function allotRoles(AllotRolesRequest $request, User $user)
    {
        $roles = $request->get('roles', []);
        $user = $this->repo->allotRoles($roles, $user);
        $user->load(['roles' => function ($query) {
            $query->select(['id', 'name', 'display_name']);
        }]);
        return $this->success($user,'修改成功');
    }

    /**
     * @param AllotPermissionsRequest $request
     * @param User $user
     * @return \Illuminate\Http\JsonResponse
     */
    public function allotPermissions(AllotPermissionsRequest $request, User $user)
    {
        $permissions = $request->get('permissions', []);
        $user = $this->repo->allotPermissions($permissions, $user);
        $user->load(['permissions' => function ($query) {
            $query->select('id', 'name', 'display_name');
        }]);
        return $this->success($user,'修改成功');
    }
}
